﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib;
using WFNetLib.PacketProc;
using NationalInstruments.UI.WindowsForms;

namespace 发电机舱
{
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        public Form2(Form1 f1)
        {
            InitializeComponent();
            form1 = f1;
        }
        public int UIIndex;        
        private void Form2_Load(object sender, EventArgs e)
        {
            if(UIIndex==1)
                label1.Text = "一号发电机组";
            else
                label1.Text = "二号发电机组";
        }
        public void Form2_ProcZS(byte[] rxList)
        {
            this.Invoke((EventHandler)(delegate
            {
                double f = BytesOP.MakeInt(rxList[3], rxList[4], rxList[5], rxList[6]);//AN0
                f = f * 0.05;
                tbZS.Text = f.ToString("f1");
                gZS.Value = f;
            }));
        }
        public void Form2_Proc4017_1(byte[] rxList)
        {
            this.Invoke((EventHandler)(delegate
            {
                double f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN0
                f = 0.0001 * (f - 500);
                form1.CheckYLAlarm(UIIndex, 0, tbJYYL, f);
                tbJYYL.Text = f.ToString("f3");
                gJYYL.Value = f;

                f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN1
                f = 0.0001 * (f - 500);
                f = 0.1;
                form1.CheckYLAlarm(UIIndex, 1, tbRYYL, f);
                tbRYYL.Text = f.ToString("f3");
                gRYYL.Value = f;

                f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                f = 0.0001 * (f - 500);
                form1.CheckYLAlarm(UIIndex, 2, tbLQSYL, f);
                tbLQSYL.Text = f.ToString("f3");
                gLQSYL.Value = f;

                f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                f = 0.05 * (f - 500);
                form1.CheckT1Alarm(UIIndex, 3, tbLQSWD, f);
                tbLQSWD.Text = f.ToString("f1");
                gLQSWD.Value = f;

                f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                f = 0.05 * (f - 500);
                form1.CheckT1Alarm(UIIndex, 4, tbRHYWD, f);
                tbRHYWD.Text = f.ToString("f1");
                gRHYWD.Value = f;
            }));
        }
        public void Form2_Proc4017_2(byte[] rxList)
        {
            this.Invoke((EventHandler)(delegate
            {
                double f = BytesOP.MakeShort(rxList[3], rxList[4]);//AN0
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 5, txPQWD1, f);
                txPQWD1.Text = f.ToString("f1");
                gPQWD1.Value = f;

                f = BytesOP.MakeShort(rxList[5], rxList[6]);//AN1
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 6, txPQWD2, f);
                txPQWD2.Text = f.ToString("f1");
                gPQWD2.Value = f;

                f = BytesOP.MakeShort(rxList[7], rxList[8]);//AN2
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 7, txPQWD3, f);
                txPQWD3.Text = f.ToString("f1");
                gPQWD3.Value = f;

                f = BytesOP.MakeShort(rxList[9], rxList[10]);//AN3
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 8, txPQWD4, f);
                txPQWD4.Text = f.ToString("f1");
                gPQWD4.Value = f;

                f = BytesOP.MakeShort(rxList[11], rxList[12]);//AN4
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 9, txPQWD5, f);
                txPQWD5.Text = f.ToString("f1");
                gPQWD5.Value = f;

                f = BytesOP.MakeShort(rxList[13], rxList[14]);//AN5
                f = 0.215 * (f - 500);
                form1.CheckT2Alarm(UIIndex, 10, txPQWD6, f);
                txPQWD6.Text = f.ToString("f1");
                gPQWD6.Value = f;
            }));
        }
        public void Form2_ProcDB(byte[] rxList)
        {
            double f;
            this.Invoke((EventHandler)(delegate
            {
                byte[] data = new byte[4];
                data[0] = rxList[3 + 3];
                data[1] = rxList[3 + 2];
                data[2] = rxList[3 + 1];
                data[3] = rxList[3 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbUa.Text = f.ToString("f1");
                gUa.Value = f;

                data[0] = rxList[7 + 3];
                data[1] = rxList[7 + 2];
                data[2] = rxList[7 + 1];
                data[3] = rxList[7 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbUb.Text = f.ToString("f1");
                gUb.Value = f;

                data[0] = rxList[11 + 3];
                data[1] = rxList[11 + 2];
                data[2] = rxList[11 + 1];
                data[3] = rxList[11 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbUc.Text = f.ToString("f1");
                gUc.Value = f;

                data[0] = rxList[27 + 3];
                data[1] = rxList[27 + 2];
                data[2] = rxList[27 + 1];
                data[3] = rxList[27 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbIa.Text = f.ToString("f1");
                gIa.Value = f;

                data[0] = rxList[31 + 3];
                data[1] = rxList[31 + 2];
                data[2] = rxList[31 + 1];
                data[3] = rxList[31 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbIb.Text = f.ToString("f1");
                gIb.Value = f;

                data[0] = rxList[35 + 3];
                data[1] = rxList[35 + 2];
                data[2] = rxList[35 + 1];
                data[3] = rxList[35 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbIc.Text = f.ToString("f1");
                gIc.Value = f;

                data[0] = rxList[51 + 3];
                data[1] = rxList[51 + 2];
                data[2] = rxList[51 + 1];
                data[3] = rxList[51 + 0];
                f = BitConverter.ToSingle(data, 0);
                f = f / 1000;
                tbP.Text = f.ToString("f1");

                data[0] = rxList[99 + 3];
                data[1] = rxList[99 + 2];
                data[2] = rxList[99 + 1];
                data[3] = rxList[99 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbQ.Text = f.ToString("f1");

                data[0] = rxList[103 + 3];
                data[1] = rxList[103 + 2];
                data[2] = rxList[103 + 1];
                data[3] = rxList[103 + 0];
                f = BitConverter.ToSingle(data, 0);
                tbF.Text = f.ToString("f1");
            }));
        }

        private void gZS_BeforeChangeValue(object sender, NationalInstruments.UI.BeforeChangeNumericValueEventArgs e)
        {
            Gauge f = (Gauge)sender;
            if (e.NewValue > f.Range.Maximum)
            {
                f.Value = f.Range.Maximum;
                e.Cancel = true;
            }
            else if (e.NewValue < f.Range.Minimum)
            {
                f.Value = f.Range.Minimum;
                e.Cancel = true;
            }
        }
    }
}
